package edu.uct.tamnd.viettourism;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import edu.uct.tamnd.viettourism.atm.MapsATM;
import edu.uct.tamnd.viettourism.citylist.CityList;
import edu.uct.tamnd.viettourism.maps.MapsNew;

public class MainActivity extends Activity {

	
	private Context mContext;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		final Button btnTraCuuDiaDanh = (Button) findViewById(R.id.btnTraCuuDiaDanh);
		final Button btnDiaDiemGanNhat = (Button) findViewById(R.id.btnDiaDiemGanNhat);
		final Button btnATM = (Button) findViewById(R.id.btnATM);
		final Button btnChiaSe = (Button) findViewById(R.id.btnChiaSe);
		final Button btnAbout = (Button) findViewById(R.id.btnAbout);
		
		btnTraCuuDiaDanh.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent i1 = new Intent(MainActivity.this, CityList.class);
				startActivity(i1);
			}
		});
		btnDiaDiemGanNhat.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i2 = new Intent(MainActivity.this, MapsNew.class);
				startActivity(i2);
			}
		});
		btnATM.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(MainActivity.this, MapsATM.class);
				startActivity(i);
				
			}
		});
		btnChiaSe.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent shareIntent=new Intent(Intent.ACTION_SEND);
		        shareIntent.setType("text/plain");
		        shareIntent.putExtra(Intent.EXTRA_TEXT, "Ứng dụng VietTourism.\n Ứng dụng du lịch cho người Việt.");
		        //shareIntent.putExtra(Intent.EXTRA_SUBJECT, Description);
		        startActivity(Intent.createChooser(shareIntent, "Chọn ứng dụng chia sẻ..."));
				   
				
			}
		});
		
		btnAbout.setOnClickListener(new OnClickListener() {
			AlertDialog alert;
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setIcon(android.R.drawable.ic_dialog_info)
				.setTitle("Thông tin ứng dụng")
				.setMessage("VietTourism ứng dụng du lịch cho người việt."
						+ "\nNgười phát triển: Nguyễn Đức Tâm");
				
				builder.setPositiveButton("OK", new AlertDialog.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
				builder.setNegativeButton("Chi tiết", new AlertDialog.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						startActivity(new Intent(MainActivity.this, HuongDanSuDung.class));
					}
				});
				alert = builder.create();
				alert.show();
			}
		});
		//checkUpdate();
		
	}
/*
	private void checkUpdate() {
		// TODO Auto-generated method stub
		checkUpdateDatabase();
		Log.d(TAG, "Sau khi checkUpdate");
	
	}
	public static void checkUpdateDatabase() {
		// if(checkVersionDatabase()){
		Log.d(TAG, "Truoc khi checkUpdate");
		String myPath = DB_PATH + DB_NAME;
		try {
				File db = new File(myPath);
				db.delete();
				Log.d(TAG, "Database is deleted");

		} catch (Exception e) {
			Log.d(TAG, "Loi delete database");
		}
				
		downloadDatabase("https://192.168.0.108/demo.db");
		
		// }else {

		// }

	}

	private boolean checkVersionDatabase() {
		// TODO Auto-generated method stub
		return false;
	}

	public static void downloadDatabase(String Url) {
		String filepath = null;
		try {
			// set the download URL, a url that points to a file on the internet
			// this is the file to be downloaded
			URL url = new URL(Url);
			// create the new connection
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

			urlConnection.setDoOutput(true);
			urlConnection.setDoInput(true);
			
            urlConnection.setInstanceFollowRedirects(true);
            urlConnection.setRequestProperty("Content-length", "0");
            urlConnection.setUseCaches(false);
            urlConnection.setAllowUserInteraction(false);
            urlConnection.setConnectTimeout(25000  milliseconds );
            urlConnection.setReadTimeout(1500010000  milliseconds );

			// set up some things on the connection
			urlConnection.setRequestMethod("GET");
			
			// and connect!
			try {
				urlConnection.connect();
			} catch (Exception e) {
				Log.d(TAG, "Loi connect http");
			}
			
			// set the path where we want to save the file
			// in this case, going to save it on the root directory of the
			// sd card.
			File SDCardRoot = Environment.getExternalStorageDirectory();
			// create a new file, specifying the path, and the filename
			// which we want to save the file as.

			String filename = "demo.db"; // you can download to any type of file
											// ex:.jpeg (image) ,.txt(text
											// file),.mp3 (audio file)
			Log.i("Local filename:", "" + filename);
			File file = new File(SDCardRoot, filename);
			if (file.createNewFile()) {
				file.createNewFile();
			}

			// this will be used to write the downloaded data into the file we
			// created
			FileOutputStream fileOutput = new FileOutputStream(file);

			// this will be used in reading the data from the internet
			InputStream inputStream = urlConnection.getInputStream();

			// this is the total size of the file
			int totalSize = urlConnection.getContentLength();
			// variable to store total downloaded bytes
			int downloadedSize = 0;

			// create a buffer...
			byte[] buffer = new byte[4 * 1024];
			int bufferLength = 0; // used to store a temporary size of the
									// buffer

			// now, read through the input buffer and write the contents to the
			// file
			while ((bufferLength = inputStream.read(buffer)) > 0) {
				// add the data in the buffer to the file in the file output
				// stream (the file on the sd card
				fileOutput.write(buffer, 0, bufferLength);
				// add up the size so we know how much is downloaded
				downloadedSize += bufferLength;
				// this is where you would do something to report the prgress,
				// like this maybe
				Log.i("Progress:", "downloadedSize:" + downloadedSize
						+ "totalSize:" + totalSize);

			}
			// close the output stream when done
			fileOutput.close();
			if (downloadedSize == totalSize)
				filepath = file.getPath();

			// catch some possible errors...
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			filepath = null;
			e.printStackTrace();
		}
		Log.i("filepath:", " " + filepath);

		Log.d(TAG, "saved: " + filepath);
	}
*/
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
}
